`U 4Li  A  LbL~| 4LYȮ(蹨(莜ȹ 4) 4 4譝NL  L4BJDE V䩛 4 4 4L܇BHI V͝LALH 4h8A ( ȹ(  B V© 7ͣ͢ש©   L9 L l` l` Έ`L2  8` }) ȹ ȹ)Χ̧`R S0`H BHIh V䬟`} ŠŠŠŠҠ A -   K:pppppF&pppppppppBppppAthecompletemachinecodetutor0!}5,"5..from-r'scollection.%7'%.%2!4)/.3/&47!2%x01Z* }X` >X)Ң L( HvH`0 1ǩ|ȩD RЍЩ./oԩ }ԩ$6%@Ԣ؝D EJKB V{{ 4  ILppB|A K:{{@ } {7 L {{{` 4 4 4L$ `é Ġ 4{   4{ 4{ 4 6{{L }{${ ' 4 4{ 4{{LF { L {{{AL [L L {' { 5{LF {0L :L {~ }{ { 4LF { { {{{{L#L {+{LF {LF *{'LF {{LF -{LF} {LF ={LF {LF {  5{{L {LLF { {{{{{{  {7L}{ 4{ ]L `  % 6{@L {){{ b& ]{{L7 n ]L{}{   L 4 4 4 4 5p"q 4 4 4p 4q{ ]  L{}{ L {0 G :A`{ș{0{`:{{80m{{L0{87m{{:{{80 m{{L\{87 } m{{:{{80m{{L{87m{{:{{80 m{{L{87 m{{`)IL\{ ũ>Ʃ} ō{ȱiȱi {L{ 4{` THE COMPLETE MACHINE CODE TUTOR **********}*********************  BY PAUL BUNN (C) 1984 NEW GENERATION SOFTWAREChoose selection by pressing }RETURN. When choice is made press the SPACE BAR. ō{{ 4 4{ 4 4{{ 4 4{ 4 4 4 4} 6 {{i{Ш{L LH{8 iŅȱŅĠÍ{ȱÅȱÅ 4{>7 {{ { 4{} L{{i{{LL~{ 4 4{ 4  4{LLåĩ<  6 Åą`PR}ESS SPACE BAR TO CONTINUE H'H{{ {0m{{{i{{Lm{0 d {L{0 {L{0  {L}`m{{{i{`v{wxx ȹ` 4 4 4w q{ 4{ 4v q{ 4{ 4 4 4yxL9 q}{ 4{ 4xyL,ȹxL9ȹxL9veyvxxL   6YL R  ҩDLwBD}EJK VBD>EHI V J L JL C:  Are you sure you want to load a stage } of lessons ? (Y/N) BLV  } LOADING ERROR. } {{{{{{ o7Gv#w }{ 4{i 4{ 4 6{{{ٽ{  {{L ;L <{ L v<{#v{{{ }{L1 @L vivwiw` 4 4 4" 5 { 4 6L v#w{AL{ v!}{#v{{{{L1 @Lv#w{#LsLNv{?v{{{{{$${{"} b{LMLY @L{{ {LM{{{{L1 {{,G ( L]Lȹ{XP#}YLN{$,ȹ{{ {L7ͅv$w{ L.{{ bL{$)ȹ{{ {:v$w{ $}L.{{ bL 3v%w{ Lv$w{ L{$'{{ b{*;v$w{ L.ȹ{{%} {L|v#w{ L|vP{Ev{{{{2{{{{{{{{hhL1 @L`{{&}{{hhL1L3{m{{{{m{i{L 4 4 4" 5 Ll{{`{` 4` 4 4'} 4{ { ~ 4 4 4{ { ~ 4! 4 4{ { ~ 4 4 4{{*i0 4 {jji0 4(} {jjjj0i 4 {**i0 4 {ji0 4 {jjji0 4{`{L 4 4 4{  ~ 4 4 4)}{  ~ 4 4 4{  ~ 4 4 4{)i0 4 4 4 4,{1 4L0 4 4 4 4{jjji0 4 *}4 4 4{jji0 4 4 4 4{*i0 4 4 4 4{**i0 4`{ q${{{{{`{JJJJ i7{Li0+}{{) i7{`i0{`{{LY0{{{{8d{{id8 {{i 8{{` 4 4 4 4`{,} 4 4 4 4L> 4 4 4 4L^ 4 4{ 4 4 4 4` 4 4{ 4 4 4 4`{ -}4{ 4{L4{(LN{$L{{ L{{ b{ );, LN{XLN{)LNROR~SBCSTAADCyAND9C<}MPEORYLDALDXORASBCSTAADCuAND5ASLCMPDECEORUINCLDALDYLSRVORAROL6RORvSBCSTASTYLDXSTXADCaAND!CMPEORALDAORA=}SBCSTAADCqAND1CMPEORQLDAORASBCSTAJMPLJSR BCCBCSBEQBMI0BNEBPLBVCPBVSp 4 4 4P{ 4{{`+>} % 4 4 4{{{{i iL%ȱȱȱ e&ȱȱ e&ȱȱ e&ȱ{ &)?}{ L) ?+`l{ 5{` %hhL {{L)` ׅ{`{{{i` {½{`@}{®{{{` &m{i` &m{L&+®{{{ȱi­{m{i` %®{{m{A}{ȱi­{` {{m{i` {{m{Lk' {{{{L+` {{{i{{q{B}L'`{)l{{{{h) {{`{{ {`{{ {`{{ {`{{ {L) 6hhL {L){`C}{L){`{L){`{-{{L){){`{ {`J{)L{h) {{`M{{L){)H(*{)LD}{h) {{`{{,{{) {h) {{`8{`{{ i{{ {L+{L+L({{ {{i{E} {L({){`{) H({) {{q{h) {{`{)H({) {jh) {{`{L){{{)M{h) {{`{{LF}){{`{ {` 4 4 4# 4 6hhL  `{ {`{L){L){{{L){L){) H({) {{G}{h) {{`{) {{8h) {{`{) {{LR*{) {{LR*{{L){{L){{L){){`{{{LH}){){`{)L*`{){{`{)@`{)`{)`{)`{)`{)@` + 4L + 6hhL ERROR - ADDRI}ESS NOT FOUND {`v+ 48 q{ 4 q{ 4{ 4`RESULTING ADDRESS $z&88+&18 89 "(88+"(J}88+"(88+&18 89'_88+"(88+'v8|88&18 8=8'_8 8+"(88+"(88+&18 89'_88+"(88+*;;+ &K}18 89"(88+"(88+"(88+&18 89 '_88+ "(88+p(999&18 89"(88+"(88+"(88+&18 89'_88+"L}(88+&):++ `(799+ "(88+"(88+(P98+`(799+(/9)98"(88+'}9|8r9`(79=8+(/9)9 8"(88+(P98+M}`(799+(/9)98"(88+*;;+ `(799+"(88+"(88+"(88+`(799+ (/9)98 "(88+y(999`(799+"(88+N}"(88+"(88+`(799+(/9)98"(88+"(88+(9 89 "(88+"(88+"(88+(9 89(9"98"(88+(v8 8O}8(9 8=8(9"9 8"(88+(.:++ (9 89(9"98"(88+*;f;+ (9 89"(88+"(88+"(88+(9 89 (9"9P}8 "(88+C)999(9 89"(88+"(88+"(88+(9 89(9"98"(88+(+;++L)998 "(88+"(88+"(8Q}8+L)998p)/9"98"(88+(}9 8r9L)99:p)/9"9 8"(88+"(88+L)998p)/9"98"(88++;;+ L)998"(8R}8+"(88+"(88+L)998 p)/9"98 "(88+)999L)998"(88+"(88+"(88+L)998p)/9"98"(88+"(S}88+4(9 89 "(88+"(88+X(9%894(9 89F(989"(88+)[:%8+"(88+)98 :"(88+X(9%894(9 89F(T}989"(88+*;9Z; 4(9 89"(88+"(88+X(9%89 4(9 89 F(989 "(88+)9%8 :4(9 89)989"(88+"U}(88+4(9 89"(88+"(88+N(f:%8=8*(f: 8J8 <(f:8=8"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+~*9 8B;*(f: 8=8V}*9 8{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+*;9P; *(f: 8J8"(88+"(88+N(f:%8J8 *(f: 8J8 <(f:8J8 "(88+W}*9:9*(f: 8J8*9l:{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+b*:%8=8p*: 8J8 "(88+"(88+b*:%8J8p*: 8JX}8 *[:8+"(88+):%8+p*: 8=8*[:8+"(88+b*:%8J8p*: 8J8 *[:8+"(88+*;:+ p*: 8J8"(88+"(88Y}+"(88+p*: 8J8 *[:8+ "(88+*9:9p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+"(88+G*:8=8#*:Z}98 "(88+"(88+G*:8J8#*:98*:8+"(88+*:8+#*:9:):++"(88+G*:8J8#*:98*:8+"(8[}8+*;:+ #*:98"(88+"(88+"(88+#*:98 *:8+ "(88+)9:9#*:98"(88+"(88+"(88+#*\}:98*:8+"(88+{HHH{Lk5{ɜ{L6ɝ{L6 LC5{L=5 {{L=5 L5γ{{ 5]}L=5{L5a8 {) { 5L+5i(ǥiȩ{L=5{{({L5hhh`ǩ|ȩȥ{{|L=5 ^}{{α{L=5ǩ|ȭ{δ{i(ǥiL5α{{ 5L=5{HHH{ L4{L5` { i0ХǍ{ȍ{_}{{|8{ {{{L5 `HH u6{ { {{L@6ILH6hh{`BD{EHI V䭴{`} {L@6{{{ `{`{`{`{) {(L5ɇ`H{ai {h{`a}HH=7ЍЩ @h  hhLbL47HU7T ԍЍh@H{ ԍb}Ѝh@L_7{{{` 4 4 4( 4{ 4 4{ 4 4{{ܩ 4 4 4ȩ 4 `셽7 c}%  6L END OF PROGRAM - PRESS A KEYaccumulator X register Y register Logical OR with number with memory locationd}Arithmetic shift left Push processor status register onto stack memory location Break - will stop executionNot a valide} instruction op-code PRESS RETURN KEY TO CONTINUEclear carry flag right left Rotate Logical AND accumulator Test BITf}s of accumulator against from stackPull Logical shift Exclusive OR set with memory locationAdd with interrupt disableg} Transfer Store at memory locationto stack pointerto accumulatorJump to subroutineJump - Load program counter with newh} addressDecrement Load stack pointer to X registeroverflow No operationIncrement decimal mode number Subtract with i}Compare not equal to zero (Z=0)equal to zero (Z=1)Branch if positive (N=0)minus (N=1)Return from subroutineto Y regisj}terset (C=1)clear (C=0)overflow flag clear (V=0)overflow flag set (V=1){{{L; 4{ 4{ 4 6` ;{k}{{{{ꭀ{ L<{{{{{ u<{݀{{{{L-<{{{{L;`R< { 4l}" 4 6hhL ERROR - DUPLICATE LABEL FOUND " 4{ 4{ 4 6` 4 4{ 4 6 LA>{ L={BJL=m}{ N{ BA$(A#0L=L=: { ,0L=:L<` { L={) ( L=n}{)( {${), 0 :L=L={),0GA:Ls=hhLN{ ,0:AGL=L=L=o}ʅ= LL>THERE SHOULD BE A SPACE BETWEEN THE MNEMONIC AND OPERAND. THE HIGHLIGHTED LINE INDICATES WHICH LINE HAS AN p}ERROR.x>  4 4{ 4 4 4 4 4 6hhL A LABEL SHOULD ONLY BE UPTO SIX LETTERS LONG. THE HIGHLIGHTEq}D LINE INDICATES THELINE WHICH HAS A LABEL TOO LONG. @pppppF&pppppppppBppppAthecompletemachinecodetutor0!s}5,"5..from-r'scollection.%7'%.%2!4)/.3/&47!2%x01Z*t}X`>c?G?h?????@1@S@r@@@@A Instructions on using this program (A Lesson 1 - The registers Gu} Lesson 2 - Memory locations H Lesson 3 - Load and store >M Exercise 1 for lesson 3 R Lesson 4 - What's v}a flag ? X Lesson 5 - Addition S Exercise 1 for lesson 5 W Exercise 2 for lesson 5 RX Lesson 6 - Sw}ubtraction Z Exercise 1 for lesson 6 ] Exercise 2 for lesson 6 ] Lesson 7 - Increments and decrements x}4^ Exercise 1 for Lesson 7 a LOAD NEXT STAGE OF LESSONS ~INSTRUCTIONS When in edit mode, press STARTy}to assemble program OPTIONto erase program andSELECTto return to the main menu. When running the program, press z}SELECTto switch between bottom and side display OPTIONto switch between HEX and DEC modes escapeto return to{} edit mode and theRETURN KEYto run each lineINSTRUCTIONS The demonstration programs will run automatically. If |}you like you can edit the program by pressing the 'escape' key. To run the program just press the key marked START. When t}}he program has finished press the SELECT key to return to the main menu. If you wish you can press the OPTION key to erase~} the program, so that you can type in your own program. When the program is running, the line highlighted is the line abo}ut to be run, with a descriptionof that line displayed near the bottom of the screen. By pressing the RETURN KEY you can ma}ke the computer run that line, and the information on the screen will be updated.INSTRUCTIONS When you are typing in} a program you can use the cursor keys (without having to press CONTROL) to edit the program. If the program line does not} have a label, first press the SPACE BAR. Once you have typed in the program, do not forget to press the key marked START }to assemble the program. If the computer reports an error, then press any key, and you will be returned to edit mode. Then} correct your error, and press START to assemble the program again. THE REGISTERS The registers are like little pigeo}n holes that can hold a number between 0 and 255. On the 6502 processor (that's the chip inside every ATARI computer) the}re are 3 registers called the accumulator, the X register and the Y register. These are usually referred to as the A,X and} Y registers. The accumulator is the most commonly used register and the other two registers are used for special purposes}.  MEMORY LOCATIONS Programs and data are stored inside the computer in MEMORY. A memory location is like a register} in the fact that each memory location can hold a number between 0 and 255. There are 65536 (64K) memory locations on the } ATARI computer of which can be RAM or ROM. RAM stands for Random Access Memory, and means that you can change the value o}f the memory location to what you want. ROM stands for Read Only Memory and you can only read its contents and writing to }it has no effect. ROM is set up at the factory andis the same each time you turn the computer on. Unlike ROM, RAM will lose} its contents if you turn the computer off, and then back on. MEMORY LOCATIONS A machine code program just affects m}emory locations. Even sound effects and high resolution graphics are controlled by changing the contents of memory locatio}ns. With machine code this can be done VERY quickly and the result of this is that arcade games and fast moving graphics a}re possible. A machine code program itself is stored in memory .A group of one to three memory locations make up an instruc}tion which is executed by the computer. LOAD AND STORE These are the simplest of all machine code instructions and th}ey just affect registers and memory locations. A load is where the contents of a register is changed to a new value. The }instructions LDX, LDY and LDA are examples of load instructions. These will load the X, Y and A registers with a new value}. The instructions STX, STY and STA are all examples of store instructions. A store instruction is where the value of a re}gister is stored at a memory location, usually for later retrieval. In the example program note that there is a BRK instr}uction. This instruction is at the end of the program, and is used to stop execution. LOAD AND STORE When running you}r own programs you may not store or load from a memory location whose address is greater than 1023 decimal. All these addr}esses are RAM, and may have their contents changed by a program. You are limited to this range of memorylocations by this} program, not by the computer. This is so that your program will NEVER crash. When writing your own programs without this p}rogram, then there IS a chance that the computer willcrash. It is up to you then to make surethat this does not happen. M}EMORY LOCATIONS Note that in the program the hash character (#) is used for a special purpose. On the 6502, usually this} character is used to represent an actualnumber. So LDA #23 will load the accumulator with 23, not the contents of memory }location 23. LDA #40 STA 1 STA 1000 LDX #5 LDY #9 STX 500 STY 600 LDA} 500 LDA 1000 LDA 600 LDA 1 BRKADDITION This is simply achieved in machine code with the }instruction ADC, this is a mnemonic meaning ADd with Carry. The carry flag is a flag which is altered depending on the res}ult of the addition. As you know, a memory locationand a register can only hold a number between 0 and 255, the carry flag }is used to indicate too large a result. For example, 240+240=480 right? Wrong. If you add 240 to 240, the result would be }224, and the carry flag will be set. The carry flag indicates that you shouldadd 256 to the result. Thus, 256+224 gives 480}, the correct result.ADDITION The instruction ADC also adds the valueof the carry flag to the result, so thatif the ca}rry is set and 0 is added to 0 then the result would be one. Therefore for true results, it is necessary to CLear the Carry }flag, by the instructionCLC. There are two demonstration programs to show ADC in use. There is also an instruction to SEt }theCarry flag, SEC. This is used in the second demonstration program to show theeffect of ADC when the carry is set. }CLC LDA #240 ADC #240 STA 200 CLC LDA #0 ADC 200 CLC ADC #1 BR}K SEC LDA #0 ADC #0 BRKWHAT'S A FLAG ? A flag is like a little indicator, like a warning }light to show low fuel on a car for example. With a flag however, the status of the flag is shown by a one or a zero. A on}e means that the flag is 'set' and a zero means that the flag is 'clear'. One flag is called the 'Zero' flag. After an in}struction, this flag could be affected depending on the instruction. If the result produced is zero, then this flag will b}e set. There are other flags which will be discussed later. When the program is running all the flags are displayed either o}n the left side or the bottom of the screen.SUBTRACTION This is achieved on the 6502 in a similar way to addition, e}xcept that theprecautionary practice is to set the carry flag instead of clearing it beforea subtraction. The instruction is} calledSBC, which stands for SuBtract with Carry. The carry is used to indicate a 'borrow'. If you subtract a number from a} smaller number then the carry flag will be cleared. If the carry is clear when you perform the SBC then the result will b}e one less than it should be. Two example programs are given to demonstrate SBC in use. SEC LDA #120 S}BC #50 STA 33 SEC LDA #80 SBC 33 SEC LDA #20 SBC #30 CLC LDA #}3 SBC #0 BRK SEC LDA #4 SBC #1 SBC #1 SBC #1 SBC #1 SBC #1 } BRKINCREMENTS AND DECREMENTS The above words mean the same as increase and decrease. However, on the 6502, incre}ments and decrements have a special meaning in machine code, and that is to increase or decrease by one. There are quite a }few of these instructions which affect registers and memory locations. An increment will add one to the contents of a memory} location and store the new result back into the memory location. The procedure is similar for registers and decrements. If} you try and decrement 0 the result will be 255, and incrementing 255 will produce a result of 0.INCREMENTS AND DECREME}NTS The following increments and decrementsare available to 6502 machine code: INCINCREMENT MEMORY LOCATION DECDEC}REMENT MEMORY LOCATION INXINCREMENT X - REGISTER DEXDECREMENT X - REGISTER INYINCREMENT Y - REGISTER DEYDECRE}MENT Y - REGISTER An example program is given to show increments and decrements in use. LDA #4 STA 230 } LDX #212 LDY #34 INC 230 DEY INX DEX INY DEC 230 DEC 230 LD}A 230 DEC 230 LDA 230 BRKɀLGŨ  GL G `Ɉ"% R}NLM 0L W @` kɈ` Z 0` ZC` WΆ`}̆ } L"pppppppppBppppAthecompletemachinecodetutor0!pppppF&pppppppppBppppAthecompletemachinecodetutor0!}5,"5..from-r'scollection.%7'%.%2!4)/.3/&47!2%x01Z*}X`>p ?3?U?x????@1@T@{@@ Lesson 8 - Transfers @ Exercise 1 for lesson 8 uB Lesson 9 - Binar}y notation B Lesson 10 - Shifts and rotates \M Exercise 1 for lesson 10 DV Lesson 11 - Two more flags W} Exercise 1 for lesson 11 [ Lesson 12 - Logical instructions S\ Exercise 1 for lesson 12 g Lesson 13 -} Indexed addressing Oh Exercise 1 for lesson 13 o LOAD NEXT STAGE OF LESSONS\TRANSFERS There are quite a} few transfer instructions on the 6502. A transfer is the copying of one value of a register into another register. The f}ollowing transfers are available TAX-TRANSFER ACCUMULATOR TO X-REGISTER TXA-TRANSFER X-REGISTER TO ACCUMULATOR TAY-TR}ANSFER ACCUMULATOR TO Y-REGISTER TYA-TRANSFER Y-REGISTER TO ACCUMULATOR There are also two other transfer instructions t}hat will be discussed later. LDA #104 TXA LDX #40 TXA CLC ADC #20 TAX } TAY LDY #3 TYA BRK BINARY NOTATION Decimal is a system of using 10 different numbers in each nu}mber column.This system is called BASE 10. However, this is not the system used by a computer, which uses BASE 2. This syste}muses only two different numbers for eachnumber column, one and zero. Because of this, the COLUMN HEADINGS will be different}. The column headings for decimal are: 1000 100 10 1 BINARY NOTATION So the decimal number 2398 is actually ca}lculated thus: 2 times 1000 = 2000 3 times 100 = 300 9 times 10 = 90 8 times 1 = + 8 } ---- Giving a result of 2398 BINARY NOTATION In binary the column headings are like this: 128 64 }32 16 8 4 2 1 Here is an example of converting binary to decimal: 1 128 0 +0 0 +0 So the binary} number 1 +16 10010111 would be 0 +0 151 (128+16+4+2+1) 1 +4 in decimal 1 +2 1 +1 } --- 151 BINARY NOTATION Each binary digit (1 or 0) is called a BIT. The previous example was an eightbit }number. The bits are numbered from right to left as BIT 0 to BIT 7. All the registers and memory locations on the 6502 are} eight bits in length. So the maximum value they can hold is 11111111 binary, or 255 decimal (128+64+32+16+8+4+2+1). A grou}p of eight bits is commonly called a BYTE. In the way binary is used above a byte could only be used to represent a posit}ive number. However, by using a technique called TWO'S COMPLEMENT negative numbers can also be stored. BINARY NOTATION } When using two's complement to represent negative numbers, bit 7 (the far left most bit of a byte) represents the sign. }On the 6502 a one is used for a negative number and a zero for a positive number. To change the sign of anumber is quite si}mple. First you have to reverse every digit so that a one becomes a zero and a zero becomes a one.Then you have to add one }for the final result. BINARY NOTATION 00011010 would be: reverse 00011010 to give 11100101 and then add }one +00000001 -------- 11100110 Therefore the negative equivalent of 00011010 is 11100110.} BINARY NOTATION Using two's complement you can use addition to simulate subtraction. Take the example of 43-1. The n}egative of positive one is 11111111. And 43 in binary is 00101011. Therefore, adding them together gives : 00101011 }+ 11111111 -------- (1)00101010 Note that there is a carry, which must be ignored if using two's complement. Soth}e result of the addition is 00101010 which is 42, the correct answer. SHIFTS AND ROTATES Shifts and rotates are instru}ctions that directly relate to changing of binary numbers. Each instruction will now be described. Note that the instruc}tion may be followed by a number representing a memory location, or the letter A representing Accumulator. The function o}f the instruction will then be carried out on the memory location or the accumulator.ROTATE RIGHT ROR *** } This instruction performs what is knownas a nine bit rotation. All the bits in the binary number are rotated one place} to the right. The carry holds the data contained in bit 0. The old data of the carry flag is then stored in bit 7. The fo}llowing example should make things clearer: BEFORE: carry number 1 01011010 AFTER: 0 1}0101101ROTATE LEFT ROL *** This instruction is also known as a nine bit rotation. It is the sam}e as theROR instruction except the bits are rotated left. The following example should make this clear: BEFORE: number } carry 01111101 1 AFTER: 11111011 0 LOGICAL SHIFT RIGHT LSR *** } This instruction is similar to the ROR instruction, except that it is a eight bit rotation (a shift). A zero enters t}he left end, bit 7, and shifts all the other numbers one place to the right. The digit that falls off the right end goes in}to the carry. The LSR instructionhas the effect of dividing a number by two, with the carry holding a result representing t}he half digit. Here is an example demonstrating LSR: BEFORE: carry number x 11100100 AFTER: 0} 01110010 ARITHMETIC SHIFT LEFT ASL *** This instruction is similar to LSR except that the b}its are shifted to the left. This has the effect of multiplyingthe number by two. If the carry is set after the instruction,} this indicates too large a result, and that 256 should be added to the result to get the correct answer. With ASL a zero} enters the far right end, bit 0, and shifts all the other bits one place to the left. What was in bit 7 is placed in the c}arry flag. BEFORE: carry number x 10000110 AFTER: 1 00001100 LDA #1 ROL} A ROL A STA 100 ROL A SEC ASL 100 LDA 100 ROR A LDA #128 ASL }A CLC LDA #2 LSR A ROR A BRK TWO MORE FLAGS THE NEGATIVE FLAG This flag always re}flects the status ofbit 7 after an instruction. If bit 7 is set after an instruction then this flag will be set. If bit 7 is }cleared after an instruction then this flag will be cleared. THE OVERFLOW FLAG This flag is set if the negative flag } has been set or cleared by an arithmeticoperation, whose result then has the wrong sign. TWO MORE FLAGS If you add two} large numbers together then there is a chance of the overflow flag being set. Take this example:- 01000011 (+67) +}01000000 (+64) --------- 10000011 (-125)  TWO MORE FLAGS In the example the two positive numbers added togethe}r have produced a negative result. If this happens then the overflow flag will become set. This flag will become set if:- } Adding large positive numbers Adding large negative numbers Subtracting a large positive number froma large negative numbe}r Subtracting a large negative number froma large positive number. Note that the instruction CLV stands for CLear oVerfl}ow flag. There is a demonstration program showing when the overflow and negative flags are set. SEC LDA #130 } SBC #20 CLC LDA #65 CLV ADC #67 SEC CLV SBC #120 CLV CL}C ADC #1 BRK LOGICAL INSTRUCTIONS Logical instructions are based on the mathematical work of George Bool}e. Each logic instruction relies on what is known as a 'truth table'. The two inputsare shown at the top, with the relating }result at the bottom. All logical instructions are performed between the accumulator and a memory location or an immediate }number. Each pair of bits are done separately by the 6502. Each logical instruction available to the 6502 will now be des }cribed. LOGICAL INSTRUCTIONS AND *** With the AND instruction, each pair of bits of the accumulator and } memory location or number are compared. If theyare BOTH ones, then the output will be one, otherwise the output will be ze }ro. Here is the truth table for AND:- Input 1: 0 0 1 1 Input 2: 0 1 0 1 ------------- Results: }0 0 0 1 An example: 10101101 ANDed with 11110001 gives: 10100001 LOGICAL INSTRUCTIONS The AND instruction is } useful when you want to 'mask off' certain bits of a binary number. For example, say bits 7 to 4 are unimportant, then you } could usethe AND instruction to erase the bits that are not wanted. The following example shows this: 11011010 - Origi }nal number 00001111 - The 'mask' byte 00001010 - The result The result now has the upper four bits missing. Note tha }t in the example programs you will have to convert from decimal to binary for the results to make sense. LOGICAL INSTRU }CTIONS ORA *** With the instruction ORA, which stands for OR Accumulator, each pair of bits are compared, } if ONE or BOTH the bits are set then the result will be a one. Here is the truth table for ORA: Input 1: 0 0 1 1 } Input 2: 0 1 0 1 ------------- Results: 0 1 1 1 LOGICAL INSTRUCTIONS Here is an example usin }g ORA: 10101011 - Number to be ORed with: 00001111 - this number gives 10101111 - this result. The ORA instruction is }useful if you want to make sure certain bits are a oneas in the previous example. LOGICAL INSTRUCTIONS EOR * }** This instruction is similar to the ORA instruction, except that if both inputs are ones, then the result will be zer }o. The truth table for EOR is: Input 1: 0 0 1 1 Input 2: 0 1 0 1 ------------- Results: 0 1 } 1 0 EOR actually stands for Exclusive OR. One of its uses is for reversing selected bits. Each bit that you want re }versed has to be in the number or the memory location. LOGICAL INSTRUCTIONS Here is an example of using the EOR instru }ction to reverse every bit. If youwere to add one to the result you would have the opposite sign of the number. Ifit had been } positive it would now be negative and vice versa. 01101110 - number to be Exclusive ORed 11111111 - with this number to }give: 10010001 - this number. LDA #1 ORA #2 ORA #4 ORA #4 AND #3 AND #2 AND } #0 LDA #255 STA 1000 LDA #190 EOR 1000 EOR #255 BRK INDEXED ADDRESSING So fa }r only three addressing modes havebeen used, IMMEDIATE, ABSOLUTE and IMPLIED. The implied form of addressing is the easiest }to understand, it is justa three letter mnemonic, like TXA. The addressing mode determines where the 6502 will fetch its da }ta from, for the instruction about to be executed. With the immediate form of addressing, the 6502 uses the actual number }specified. So if the instruction was LDA #20, the 6502 would load the accumulator with thenumber 20, not the memory locatio }n. INDEXED ADDRESSING With absolute addressing the number given after the instruction is a memory location. With the }instruction LDA 20 the 6502 will load the accumulator with the number contained in the memory location number 20, which cou }ld be any number between 0 and 255, which need notbe the actual number 20. INDEXED ADDRESSING Indexed addressing is si }milar to absolute addressing in the fact that the6502 fetches the data from a memory location. The X and Y registers are s }ometimes called index registers, because of their use with this form of addressing. Two examples of using indexed addressi }ng are about to be given, with an explanation showing how they are used, and how they work. INDEXED ADDRESSING One ex !}ample of using indexed addressingis LDA 710,X and another example is ORA 654,Y. In the first example the contents of the X "}register are added to 710 to give a memory location address. So, if the X register holds 25 when the program is running the #}n the 6502 will load the accumulator with the contents of memory location 735 (710+25). Similarly, with the second example $} the Y register is added to 654 to give a memory location address. If the Y register contains 216 when the program is run %}ning then the accumulator will be ORed against memory location 870 (654+216). An example program shows indexed addressing &}in use. LDA #6 STA 605 CLC ADC #2 STA 606 ADC #2 STA 607 LDX #0 '} TXA LDA 605,X INX ADC 605,X INX ADC 605,X BRK) `h  (} Ť ɡ}àL3 6 6 6 RLȩ נ נ נ̆  L"r0!\pppppF&pppppppppBppppAthecompletemachinecodetutor0! *}5,"5..from-r'scollection.%7'%.%2!4)/.3/&47!2%x01Z* +}X`>|l?C?f?????@/@R@{@@@@ A Lesson 14 - Branches and jumps .A Exercise 1 for lesson 14 RG ,} Exercise 2 for lesson 14 G Exercise 3 for lesson 14 H Lesson 15 - Compares H Exercise 1 for lesson 1 -}5 P Exercise 2 for lesson 15 Q Lesson 16 - Subroutines WR Exercise 1 for lesson 16 %T Lesson 17 - H .}exadecimal notation T Exercise 1 for lesson 17 e Exercise 2 for lesson 17 f Lesson 18 - Binary Coded De /}cimal eg Exercise 1 for lesson 18 k LOAD NEXT STAGE OF LESSONS\ BRANCHES AND JUMPS Jump instructions are 0}like the GOTO statement in BASIC, and the branch instructions are like IF..THEN..GOTO statements in BASIC. Previously yo 1}u have been pressing the space bar before typing in a program line. You have been skipping the LABEL FIELD each time you h 2}ave done so. If youwant to use a jump or a branch to a linedo NOT press the space bar at the start of the line, but type in a 3} label of up to 6 characters long and then press the space bar. A label can then be used by a program for jumps and branche 4}s. Each jump and branch will now be described. BRANCHES AND JUMPS JMP- JUMP (UNCONDITIONAL) BEQ- BRANCH IF ZERO F 5}LAG SET BNE- BRANCH IF ZERO FLAG CLEAR BCS- BRANCH IF CARRY FLAG SET BCC- BRANCH IF CARRY FLAG CLEAR BVS- BRA 6}NCH IF OVERFLOW FLAG SET BVC- BRANCH IF OVERFLOW FLAG CLEAR BMI- BRANCH IF NEGATIVE FLAG SET BPL- BRANCH IF NEGAT 7}IVE FLAG CLEARTHE DEMONSTRATION PROGRAMS EXERCISE 1 This is a simple program to use the X register to count down fr 8}om 10 to 0. BEQ is used to detect when X reaches 0, and JMP is used to jump round a loop until X equals 0. EXERCISE 2 T 9}his program will divide 58 by 5. The X register holds the answer, and at the end the Y register holds the remainder. EXERCI :}SE 3 This program will multiply the number contained in the X register at the start(in this case 5) by the number in the Y ;} register at the start (in this case 6) You can easily change the program to multiply any two numbers (as long as theansw <}er does not exceed 255). LDX #10 LOOP DEX BEQ END JMP LOOP END BRK LDA #58 SEC AGAIN =} SBC #5 BMI FINISH INX JMP AGAIN FINISH ADC #5 TAY BRK LDX #5 LDY #6 >}LDA #0 STA 20 LINE1 TYA CLC ADC 20 STA 20 DEX BNE LINE1 JMP DUMMY DUMMY2 JM ?}P END DUMMY JMP DUMMY2 END BRKCOMPARES So far branches have only been used for detecting whether flags are set or @} clear. This is, in essence, all that branches can do, but with compares, theycan do a lot more. A compare is really a sub A}traction that affects the flags andleaves the accumulator in its original condition. The negative, overflow, zero and carry B}flags are all affected after a compare, how they are affected will bediscussed shortly.COMPARES As previously mention C}ed, a compare is really a subtraction, leaving the accumulator intact. Therefore, a comparewill affect the flags in exactly D} the same way. With a compare however, it is not necessary to set the carry flag beforehand, as it is with a subtraction. E} You may perform a compare between any of the three registers and a memory location or an immediate number. The instructio F}n CMP is used to compare the accumulator, and the instructions CPX and CPY are used to compare the X and Y registers respec G}tively.COMPARES The zero flag will become set if the compare should prove both numbers equal,otherwise it will be cle H}ared. The carry flag will be set if the number specified with the compare is less than or equal to the register used by I} the compare. The carry flag willbe cleared otherwise. The negative flag will be set if the subtraction (compare) produce J}s a negative result, otherwise it will be cleared. The overflow flag will be set or cleared depending on the subtractio K}n using the rules for two's complement as previously discussed. COMPARES There are two example programs showing the L} compare instructions when working. Look particularly at the flags,and how they are affected by the compareinstructions. Try M} changing the programs yourself, to see what happens. A SUMMARY OF INSTRUCTIONS: CMPCOMPARE ACCUMULATOR WITH NUMBER N} OR MEMORY LOCATION CPXCOMPARE X-REGISTER WITH NUMBER OR MEMORY LOCATION CPYCOMPARE Y-REGISTER WITH NUMBER O} OR MEMORY LOCATIONSTART LDX #50 LOOP1 DEX CPX #40 BEQ END1 JMP LOOP1 END1 DEY CPY #254 P} BNE START CLV CLC LDA #156 CMP #100 BVS ALWAYS NEVER BRK ALWAYS CMP #1 BVC Q}NEVERPROG2 LDX #230 STX 300 LDY #0 LAB1 LDA #230 CMP 290,Y BEQ FOUND INY JMP LA R}B1 FOUND CLC LDX #4 LAB2 CPX #2 BCC END1 DEX JMP LAB2 END1 BRKSUBROUTINES The instruc S}tion JSR (followed by a label name) is effectively the same as the GOSUB command from BASIC. When theJSRinstruction is e T}xecuted, controlis passed to the line with that label on it. When the subroutine has finished,the instructionRTS(ReTurn fr U}om Subroutine) should be executed. This instruction will return to the line after the JSR instruction. There is an exam V}ple program showing the instructions JSR and RTS in use. LDA #0 LDX #5 LOOP JSR ADD8 DEX BNE L W}OOP BRK ADD8 CLC ADC #8 RTS THIS PROGRAM WILL MULTIPLY FIVE BY EIGHT.HEXADECIMAL Like binary X}and decimal, hexadecimal is a number base. Where binary is base 2and decimal is base 10, hexadecimal is base 16. A number b Y}ase relates to the number of different digits used in each of the column headings. With decimal this is ten digits, the nu Z}mbers 0 to 9. Hexadecimal also uses these digits, but because it uses sixteen different digitsfor each column heading, six [}more characters are used to represent the number digits 10,11,12,13,14 and 15. The standard way to represent these is to \}use A,B,C,D,E and F respectively.HEXADECIMAL So in actual fact, the hexadecimal number 12 15 11 4 could easil ]}y be misunderstood as 12 15 1 14 for example. This is why the letters A to F are used to represent the numbers 10 t ^}o 15. Using letters the above number would be CFB4. The column headings for hexadecimal are: 4096 256 16 1HE _}XADECIMAL This means that CFB4 in decimal is calculated thus: C = 12 * 4096 = 49152 F = 15 * 256 = 3840 B `} = 11 * 16 = 176 4 = 4 * 1 = + 4 ----- CFB4 is therefore 53172 in decimal. a}HEXADECIMAL As you have seen, a hexadecimal number bears hardly any resemblance to a decimal number. However, a binary n b}umberbears a direct resemblance. Each group of four bits of a binary number (called a nybble) represents one hexadecimal d c}igit. Here is the table for conversion: DECIMAL BINARY HEX DECIMAL BINARY HEX 0 0000 0 8 1000 8 1 d} 0001 1 9 1001 9 2 0010 2 10 1010 A 3 0011 3 11 1011 B 4 e}0100 4 12 1100 C 5 0101 5 13 1101 D 6 0110 6 14 1110 E 7 0111 f} 7 15 1111 F HEXADECIMAL Using the given table, it is easy to convert from binary to hexadecimal (hex)or g} vice versa. Also, using hex makes two's complement easier to interpret. By pressing the red function key f3 youcan switch q}BkMCTUTOR PGRbNrLESSONS 1..biLESSONS 8..b`)LESSONS 14.b{LESSONS 19.C FRANCOISPIC between hexadecimal and decimal modes. This affects whether the values of the A, X and Y registers are displayed in hex o r}r decimal. When in hex mode the '$' character is used to show a hex number. You can also use this character in a program s}to specify a hex number as well.HEXADECIMAL By now you must be well aware that a memory location and a register can o t}nly hold a number between 0 and 255, which is an eight bit binary number. In hex this is a number between $0 and $FF. How u}ever, you can use two consecutive memory locations to give 16 bits which can represent one number. This two byte storage h v}as the capability to represent 65536 (256*256) different numbers. When using two memory locations to store one number, th w}e memory location with the lowest address stores what is known as the Least Significant Byte (LSB), the next sequential by x}te stores what is known as the Most Significant Byte (MSB). Therefore, if the LSB holds $A1 and the MSB holds $4E then the y} resulting number will be $4EA1.HEXADECIMAL When in hex mode, when a load or store is performed on a memory location z}, the computer will print 'RESULTING ADDRESS:'followed by a hexadecimal number. This is the actual address of a memory loc {}ation the computer uses for the instruction, even for indexed addressingand the like. There are two example programs using |} hexadecimal numbers, to help demonstratewhat has been discussed. Remember to press function key f3 to put the displayin he }}xadecimal mode. The first program is a 16 bit multiplication program that will multiply the value of the X and Y registers ~} together. The result is storedat consecutive memory locations in LSB MSB format. The second program is a simple 16 bit sub }traction program.A PAGE A page is a natural follow on from the definition of a hexadecimal number. A page is a commo }nly used term to describeAREAS of memory. It is simply the MSB of a memory address. For example, memorylocation $1EA is desc }ribed as being in page 1. Any memory location between addresses 0 and 255 will therefore be in page zero. Because you can }only use memory locations with addresses between 0 and 1024 (0 and 400 hex) this means you can only use 4 pages with this } simulator. On a normal 6502 system without this simulator you have 256 pages of RAM and ROM (addresses 0 to $FFFF). } LDX #$E9 LDY #$A LDA #0 STA 30 STA 31 LOOP TXA CLC ADC 30 STA 30 }LDA 31 ADC #0 STA 31 DEY BNE LOOP LDX 30 LDY 31 SEC LDA #$2D S }BC #$E2 STA 450 LDA #$3A SBC #$38 STA 451 LDX 450 LDY 451 BRKBINARY CODE }D DECIMAL (BCD) This is a system where decimal numbers are represented using hexadecimal numbers. For example, normally t }he hex number $25 is 2*16+5 (37) in decimal. However, using BCD this hex number wouldactually represent the DECIMAL number } 25. When using BCD, results will be meaningless if looking at the results in decimal or hex without knowing you are using } BCD. To operate BCD the 6502 instruction SED, SEt Decimal mode, will set the decimal mode flag. To CLearthe Decimal mode f }lag, use the instruction CLD. Once decimal mode is activated all ADC and SBC instructions will be carried out in BCD.B }INARY CODED DECIMAL (BCD) If you try to add using BCD, and the result is greater than hexadecimal 99 then the carry flag } will be set. If you perform a subtraction and the result is less than or equal to 0, then the carry flag will be cleared. } There is an example program using BCD which should clarify what has been discussed. Remember to press function key f3 t }o enter hex mode, otherwise the results will be meaningless. SED LDA #$80 ADD1 CLC ADC #1 BCC A }DD1 LDA #$95 TAKE5 SEC SBC #5 BCS TAKE5 CLD LDA #242 NORMAL CLC ADC #1 CMP }#$1B BNE NORMAL BRKe ̆  L"etemachinecodetutor0! ApppppF&pppppppppBppppAthecompletemachinecodetutor0!}5,"5..from-r'scollection.%7'%.%2!4)/.3/&47!2%x01Z*}X`>|y ?4?W?????@8@[@z@@ Lesson 19 - The stack @ Exercise 1 for lesson 19 &O Lesson 20 - Mo}re addressing modes O Exercise 1 for lesson 20 ` Exercise 2 for lesson 20 ` Lesson 21 - More on the sta}ck a Exercise 1 for lesson 21 h Lesson 22 - The BIT instruction i Exercise 1 for lesson 22 l Less}on 23 - Interrupts m Lesson 24 - What now ? s LOAD A DIFFERENT STAGE OF LESSONS \THE STACK The stack is }like a pile of plates stacked one on top of the other that can be upto 256 plates high. You can add plates to the top or r}emove plates from the top. If you want a plate near the bottom, you must start from the top taking off all the plates, thr}owing themaway until you get to the plate that youwant.THE STACK The stack is one page of memory that resides at page} 1 ($100 - $1FF). There is a STACK POINTER (SP) which always points to the top of the stack. It is a one byte value and is} used in a similar way to indexed addressing where the stack pointer is added to $100 to give an address that points to a }data item. Each time you PUSH an item (put anitem on the stack) or PULL an item (remove a data item from the stack), thesta}ck pointer is updated. It starts off as $FF, pointing to $1FF, and then when data items are added, it decreases, and when i}tems are pulled from the stack it increases. Just like a memory location the stack pointer will wrap round if too many ite}ms are pulled or pushed.THE STACK All programs used with this simulator are stored in memory locations $7B00 onwards}. It takes between one and three bytes (called op-code bytes if used for execution of a program) to make up an instruction}. You can see the addresses and op-code bytes for each instruction displayed if you want, by pressing the yellow key marke}d 'SELECT' when the program is running. To revert to the normal mode press 'SELECT' again.THE STACK The stack is par}ticularily useful in the temporary storage of data. The 6502 uses it itself. How it does so will be discussed a little lat}er on. To keep track of which instruction is being executed a PROGRAM COUNTER (PC) isused. This makes sure that instructio}ns are executed in the right order and it copes with jumps, branches and subroutines. It is a 16 bit number contained ins}ide the 6502.THE STACK After each instruction the program counter is updated to point to the next instruction to be }executed.Normally this means that the program counter is increased by the number of bytes in an instruction. With jumps it } is loaded with a complete new value. With branches (relative jumps) there is an offset byte which is added to the program} counter. It can also be a negative number if bit 7 is set (using two's complement). Therefore a branch can only change th}e program counter by 127 forwards or 128 backwards. This is alimitation when writing a long program but should not affect y}ou when writing programs with this simulator.THE STACK One of the ways the 6502 uses the stackis to ensure that JSR i}nstructions work properly, and that when a RTS instruction is executed, control is passed to the instruction immediately }following the JSR instruction. This is done by pushing the contents of the program counter onto the stack. This is always }the address of the memory location where the op-code for the JSR instruction is plus two. Then, when the RTS instruction i}s executed, this 16 bit number is then pulled off from the stack and one is added to it, and this value is put into the pr}ogram counter. This has the effect of the nextinstruction executed being the one immediately after the originating JSR ins}truction.THE STACK Here is a list of instructions that are associated with the stack PHA- PUSH ACCUMULATOR ONTO S}TACK PLA- PULL ACCUMULATOR FROM STACK TSX- TRANSFER VALUE OF STACK POINTER TO X REGISTER TXS- TRANSFER VA}LUE OF X REGISTER TO STACK POINTER A demonstration program is given to showthe effect of these and the JSR and RT}S instructions on the stack. TSX JSR NORET RET LDA #$7B PHA LDA #$12 PHA TSX } RTS NORET PLA TSX PLA TSX JMP RET END TSX BRK MORE ADDRESSING MODES So far th}e following addressing modes have been used: IMPLIED These instructions are just three letters on their own, performing} a simple task, like TXS for example. They only require one byte per instruction. ABSOLUTE These are simple instructions} that affect memory locations whoose address is greater than 255 (page 1 or above). Three bytes are used per instruction.} MORE ADDRESSING MODES ZERO PAGE This mode is exactly the same as the absolute mode, except that only two bytes are }used per instruction. This is because the MSB is assumed to be 0 as all memory locations using this form of addressing are} on page 0. IMMEDIATE This mode also requires two bytes for each instruction. Immediate is where an actual number is gi}ven instead of a memory location address. The '#' symbol is used to specify this mode of addressing with assemblers (inclu}ding this one). MORE ADDRESSING MODES ABSOLUTE,Y This is the same as absolute addressing except that the contents of }the Y register are added to the specified address to give a resulting address of a memory location. ABSOLUTE,X This is} the same as ABSOLUTE,Y addressing except the X register is added instead of the Y register. MORE ADDRESSING MODES ZE}RO PAGE,Y This is the same as ABSOLUTE,Y except that a zero page address is given and the instruction only takes two byte}s as opposed to three needed by ABSOLUTE,Y addressing. ZERO PAGE,X This is the same as ZERO PAGE,Y form of addressing e}xcept the X register is used instead of the Y register. This mode also only requires two bytes.POST INDEXED ADDRESSING} This mode of addressing is very useful indeed. An example of using this mode of addressing is STA (78),Y. When this ins}truction is executed, the 6502 will look up locations 78 and 79 in zeropage. Location 78 will form the LSB of the address a}nd location 79 will form the MSB of the address. Therefore if location 78 were to hold hex 13 and location 79 were to hold} hex 2F when the program was running then the resulting address of the memory locationwould be hex 2F13. After this address} has been calculated the Y register is added to it to form the final address. So if the Y register held 5 when the 6502 t}ried to execute the above instruction, the 6502 would store the accumulator at memory location hex 2F18.PRE-INDEXED IN}DIRECT ADDRESSING This is a very rarely used form of addressing. It will be described here for completeness. The instructi}on LDA (45,X) will be used as an example to explain this form of addressing. Whatever is contained in the X register at t}he time of execution is added to thezero page address in the instruction, inthis case 45. So if X contained 20 when the prog}ram is running, the final zero page address is 65. This location then forms the LSB of the final address and location 66 f}orms the MSB of the final address. Therefore if X contained 5 when the program was running and location 65 contained hex E}2 and location 66 contained hex A1 then the accumulator will be loaded with the contents of memory location A1E2 in the a}bove example. MORE ADDRESSING MODES INDIRECT This form of addressing is NOT supported by the simulator. However, it } would be supported by a normal assembler. There is only one instructionthat uses indirect addressing and that is JMP (xxxx}). This will take the contents of memory location xxxx and that will form the LSB of the JMP address. Then the contents of} memory location xxxx+1 forms the MSB of the JMP address. The program counter is thenloaded with the JMP address causing t}he next instruction to be executed, theinstruction held in the memory location pointed to by xxxx. MORE ADDRESSING MODES } Consider the following program: LDA #9 STA 320 LDA #2 STA 321 JMP (320) Although this program will not work on thi}s simulator, it would be assembled and run on a normal assembler and 6502. This program would have the effect of loading t}he program counter with hex 209. The next instruction to be executed would be at memory location 209. LDA #2 } STA 32 LDA #2 STA 33 AGAIN LDY #1 LOOP TYA LOOP2 STA (32),Y INY CPY #10 BNE LOOP } INC 33 LDA 33 CMP #3 BEQ AGAIN BRK LDA #$F1 STA 140 LDA #3 STA 141} STA 149 STA 150 LDX #10 TXA STA (130,X) LDA #40 LDX #19 STA (130,X) } LDX #10 EOR (130,X) BRK MORE ON THE STACK The flags are held in one byte in the 6502 and are arra}nged thus: BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 N V - B D I Z C N - NEGATIVE FLAG V - OVERFLOW} FLAG - - NOT USED (EXPANSION BIT) B - BREAK FLAG (NOT USED BY SIMULATOR) D - DECIMAL MODE FLAG I - INTERRUPT DISABLE FLA}G Z - ZERO FLAG C - CARRY FLAG MORE ON THE STACK Therefore, if you think of all the flagsas a single 8 bit binary numb}er, that is one byte, each bit represents one number from the binary column headings. For example, the overflow flag gives } the number 64. MORE ON THE STACK The flags are stored in this way and are kept track of by the 6502 processor and st}ored in what is known as the PROCESSOR STATUS REGISTER (PSR). As you know, most instructions affect flags, but only two in}structions affect the processor status register as a whole. These instructions are PHP and PLP, which will PUSH and PULL t}he processor status register to and from the stack. If you wanted to set, say three flags at once, you could use PHP and P}LP to do so. However, there are no transfer instructions to transfer between the accumulator and PSR, so other instructio}ns have to be used in conjunction with these instructions for them to be effective. MORE ON THE STACK An example prog}ram demonstrating how the processor status register is used in conjunction with the stack is given to help demonstrate its} uses. One of the other uses for these instructions is the temporary saving of the flags. You can push the value of thePS}R onto the stack by using PHP, execute instructions that normally affect the flags. Then by using PLP you can restore the }flags to their original values. Remember to pull and push the right number of values, otherwise the stack will become fill}ed up and 'dirty'. SEC TSX PHP PLA ORA #192 PHA PLP LDA #$FF P}HA PLP LDA #1 LOOP PHA PLP ASL A BCC LOOP BRK THE BIT INSTRUCTION This inst}ruction is used to check that one or more BITs of a memory location are set. The memory location is either in the addressi}ng modes absolute or zeropage. The bits to be tested are in the accumulator, and the memory location to be tested against t}he accumulator is supplied with the BIT instruction in the form BIT xxxx where xxxx is a singlememory location. After the i}nstruction the zero flag will be set if none of thebits specified by the accumulator are set. The accumulator is left uncha}nged after the instruction. The contents of bit 6 of the memory location are placed in the overflow flag, and bit 7 is pl}aced in the negative flag. An example program is given to show BIT in use. LDA #$30 STA 6 LDA #0 } SEC ROTATE ROL A BIT 6 BEQ ROTATE LDA #$C0 STA $3F0 LDA #0 BIT $3F0 LDA #$8}0 STA 560 LDA #0 BIT 560 BRKINTERRUPTS An interrupt is something that occurs after a fixe}d amount of time or after a certain event, like pressing BREAK forexample. When an interrupt occurs the 6502 looks up what }is known as an INTERRUPT VECTOR which is an indirect jump to a small program that handles theinterrupt. This interrupt migh}t be to read the keyboard for example. When the interrupt has finished what it is doing the program will resume as normal }to where it left off before the interrupt occurred.INTERRUPTS With the simulator, you can not write your own interr}upt routines. However, interrupts are occurring all the time, to check if you press BREAK or press a key. There are two ty}pes of interrupts these are: MASKABLE INTERRUPTS These are interrupts that can be changed to point to your own routine}. By using the instruction SEI (SEt Interrupt disable flag) you can mask out maskable interrupts altogether, so that mask}able interrupts do not occur until a CLI (CLear Interrupt disable flag) instruction is executed.NON - MASKABLE INTERRU}PTS This type of interrupt is not affected by the SEI and CLI instructions. Depending on the computer, these interrupts} can still have their vectors changed. All interrupts end in an RTI (ReTurn from Interrupt) instruction. Usually an int}errupt routine has to save the registers it uses, by pushing them onto the stack. When an interrupt occurs the processor s}tatus register and the returnaddress are pushed onto the stack. When an RTI instruction is executed the flagsand return addr}ess are pulled from the stack and the program will continue as normal.WHAT NOW ? You have now finished the course of} lessons to teach you 6502 machine code. The lessons have been designed to teach machine code so that it will be the samefo}r each 6502 based machine. However, to make the most of the machine, it is advisable to use what is called an OPERATING SY}STEM. This is contained in ROM and given with the computer fitted as standard. An operating system is a machine code progr}am that lets you use advanced features of a machine like graphics and sound. It also handles routines for reading the keyb}oard and using cassettes and disc drives, as wellas many other functions.WHAT NOW ? As this program only simulates th}e running of a 6502 program, you cannot use this program in conjunction with the operating system. Therefore, it is time }to use a more powerful but less user friendly assembler. Note that machine code programs run veryquickly and will not wait }for you to press the return key like this one does!With a machine code program not written with this assembler there is a c }hance that the program could 'crash'. Therefore it is up to you to make sure you use 'safe' areas of memory. If you are e }ver unsure of a instruction you can always use this program to check how it works before using it in your own program.AS }ROLSRO% .DEƽ<><.O.AN.NOԡ.RE .REF.DEF.NOT .AND .OR ,X̆ }  L"pppppppBppppAthecompletemachinecodetutor0!((ʔ           *# ^ ;#/ &.* * }#^.    C      (   }    *  (    (    *     }              *  * (  } *(    *(** *(* * }*  *  *   *         *}   (  (*   (( }  (           } * (     "       *((  * } * (          }(     * ( *      }  * (( *   (    }((  (( (     " ( "   }  (  ("  * (    (""  }